package com.example.coursehub.classmanagement.repository;

import com.example.coursehub.classmanagement.entity.Class;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 班级数据访问接口
 */
@Repository
public interface ClassRepository extends JpaRepository<Class, Long> {
    
    /**
     * 根据课程ID查找班级
     */
    List<Class> findByCourseId(Long courseId);
    
    /**
     * 根据教师ID查找班级
     */
    List<Class> findByTeacherId(Long teacherId);
    
    /**
     * 根据班级名称模糊查询
     */
    @Query("SELECT c FROM Class c WHERE c.name LIKE %:name%")
    List<Class> findByNameContaining(@Param("name") String name);
    
    /**
     * 查找有空位的班级
     */
    @Query("SELECT c FROM Class c WHERE c.currentStudents < c.maxStudents")
    List<Class> findAvailableClasses();
    
    /**
     * 统计教师的班级数量
     */
    long countByTeacherId(Long teacherId);
    
    /**
     * 统计课程的班级数量
     */
    long countByCourseId(Long courseId);
}